<template>
  <div class="container">
    <el-card class="box-card">
      <div slot="header" class="card-header clearfix">
        <h5 class="title pull-left">用户信息</h5>
        <el-button
          type="text"
          icon="el-icon-edit"
          size="small"
          class="btn pull-right"
          @click="toUserEdit"
        >编辑</el-button>
      </div>
      <div class="card-wrapper">
        <el-row>
          <el-col :span="6"><strong>姓名：</strong>{{ renderData.name }}</el-col>
          <el-col :span="6">
            <strong>性别：</strong>
            <span v-if="renderData.sex === 0">女</span>
            <span v-else>男</span>
          </el-col>
          <el-col :span="12"><strong>VeevaCode：</strong>{{ renderData.veevacode }}</el-col>
        </el-row>
        <el-divider />
        <el-row>
          <el-col :span="24">
            <strong>等级：</strong>
            <el-tag
              v-for="item in renderData.gradeList"
              :key="item.id"
              effect="plain"
            >{{ item.productName }}: {{ item.grade }}</el-tag>
          </el-col>
        </el-row>
        <el-divider />
        <el-row>
          <el-col :span="12"><strong>医院名称：</strong>{{ renderData.hospital }}</el-col>
          <el-col :span="12"><strong>科室名称：</strong>{{ renderData.department }}</el-col>
        </el-row>
        <el-divider />
        <el-row>
          <el-col :span="12"><strong>创建于：</strong>{{ renderData.settime | moment('YYYY-MM-DD HH:mm:ss') }}</el-col>
          <el-col :span="12"><strong>更新于：</strong>{{ renderData.uptime | moment('YYYY-MM-DD HH:mm:ss') }}</el-col>
        </el-row>
      </div>
    </el-card>

    <!-- <el-card class="box-card mt40">
      <div slot="header" class="card-header clearfix">
        <h5 class="title pull-left">关联岗位</h5>
      </div>

      <el-form ref="filterForm" :inline="true" class="filter-container">
        <el-form-item prop="关联岗位">
          <el-select
            v-model="postId"
            filterable
            remote
            reserve-keyword
            clearable
            size="small"
            placeholder="请输入岗位编码"
            :remote-method="remotePosts"
            :loading="loading"
          >
            <el-option
              v-for="item in postOptions"
              :key="item.id"
              :label="`${item.code} - ${item.name}`"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button
            size="small"
            type="success"
            icon="el-icon-plus"
            @click="addTerritory"
          >添加岗位</el-button>
        </el-form-item>
      </el-form>

      <el-table
        :data="renderData.postList"
        stripe
        fit
        highlight-current-row
      >
        <el-table-column label="ID" width="80" align="center">
          <template slot-scope="scope">
            {{ scope.row.id }}
          </template>
        </el-table-column>
        <el-table-column label="CODE" width="80" align="center">
          <template slot-scope="scope">
            {{ scope.row.code }}
          </template>
        </el-table-column>
        <el-table-column label="岗位">
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" align="center">
          <template slot-scope="scope">
            <el-button
              type="danger"
              size="mini"
              @click="removeTerritory(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card> -->
  </div>
</template>

<script>
import {
  getHcpUsersList,
  addTerritory,
  removeTerritory
} from '@/api/hcpUsers'
import {
  findTerritory
} from '@/api/territories'

export default {
  name: 'HcpUserShow',
  data() {
    return {
      loading: false,
      hcpId: '',
      postId: '',
      renderData: {},
      postOptions: []
    }
  },
  watch: {
    $route: {
      handler(route) {
        this.hcpId = route.params && route.params.id
      },
      immediate: true
    }
  },
  created() {
    this.hcpId = this.$route.params && this.$route.params.id
    this.fetchData(this.hcpId)
  },
  methods: {
    fetchData(id) {
      getHcpUsersList({ id }).then(response => {
        const { data } = response
        // console.log(data)
        this.renderData = data[0]
      }).catch(err => {
        console.log(err)
      })
    },
    remotePosts(query) {
      if (query !== '') {
        this.loading = true
        setTimeout(() => {
          this.loading = false
          findTerritory({ code: query }).then(response => {
            const { data } = response
            // console.log(data)
            this.postOptions = data
          }).catch(err => {
            console.log(err)
          })
        }, 200)
      } else {
        this.postOptions = []
      }
    },
    addTerritory() {
      addTerritory({
        hcpid: this.hcpId,
        postid: this.postId
      }).then(response => {
        // console.log(response)
        this.fetchData(this.hcpId)
        this.$message({
          type: 'success',
          message: '添加成功!'
        })
      }).catch(err => {
        console.log(err)
      })
    },
    removeTerritory(id) {
      this.$confirm('此操作将永久删除该岗位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeTerritory({ id }).then(response => {
          // console.log(response)
          this.fetchData(this.hcpId)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(err => {
          console.log(err)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    toUserEdit() {
      this.$router.push(`/hcp-users/edit/${this.hcpId}`)
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding: 40px 140px;
}
</style>
